home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 551 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.3 KB  |  81 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: OSTRSTREAM and sizing
  5. Date: 26 Feb 1996 11:49:27 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <9602261753.AA28370@lts.sel.alcatel.de>
  9. References: <4glcfq$sfr@venus.roc.csci.csc.com> <4glkpb$c8q@engnews1.Eng.Sun.COM>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Mon, 26 Feb 96 18:53:00 +0100
  12. In-Reply-To: clamage@Eng.sun.com's message of 24 Feb 1996 17:15:11 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMTIO20y4NqrwXLNJAQGSxgH/ZsvP07eFPwp83yHns8NossmaKK/g9/qn
  15.     o7RnSMxQGmgoH5uo/OegLIlN/M5DjsMBVa3UqBi3pphqTQxYhodSWw==
  16.     =xnRy
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article <4glkpb$c8q@engnews1.Eng.Sun.COM> clamage@Eng.sun.com
  20. (Steve Clamage) writes:
  21.  
  22. |> In article sfr@venus.roc.csci.csc.com, tottinge@csci.csc.com (Tim Ottinger) writes:
  23.  
  24. |> >This might be a silly question, but a coworker of mine noticed this, and we've 
  25. |> >tested it on two compilers with similar results:
  26.  
  27. |> >    #include <iostream.h>
  28. |> >    #include <strstream.h>
  29. |> >    int main (char * argc, char * argv[])
  30. |> >    {
  31. |> >       ostrstream str("abcdefg", 4);
  32. |> >   
  33. |> >       cout << "Length = " << str.pcount() << endl;
  34. |> >       cout << "Data   = " << str.str();
  35. |> >    }
  36.  
  37. |> >produces the following output on both the GNU and HP C++ compilers
  38. |> >    Length = 0
  39. |> >    Data   = abcdefg
  40.  
  41. |> >Two things were puzzling about this.  
  42.  
  43. |> >The first was that the length parameter on the c'tor didn't seem 
  44. |> >to be used for anything, and the second was that I couldn't spot the 
  45. |> >right way to tell how big the buffer in the strstreambuf is (how many 
  46. |> >actual characters are in it).
  47.  
  48. |> In the example code, you create an ostrstream, and pass in a fixed
  49. |> buffer. You tell the ostrstream that the buffer can hold 4 characters,
  50. |> including a terminating null, if any. You don't write anything to
  51. |> the stream.
  52.  
  53. Just one small point which Steve forgot to mention (since it wasn't
  54. really relevant to the question you asked): you'd better not try and
  55. write anything to this stream, either, since it will result in
  56. attempting to modify a string literal (undefined behavior according to
  57. the standard, a segment violation in better compilers).
  58.  
  59. An interesting question: is ostrstream allowed to modify the parts of
  60. the buffer which are presumed unwritten?  Could a legal implementation
  61. of ostrstream, for example, start by zapping the buffer to '\0'?  If
  62. this is the case, the above example invokes undefined behavior even
  63. without writing to the stream.
  64.  
  65. (It's worth noting that in Steve's example of how to correctly use the
  66. functions, the buffer is declared explicitly as an array, in order to
  67. avoid the undefined behavior.)
  68.  
  69. --
  70. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  71. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  72. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  73.                 -- A la recherche d'une activitΘ dans une region francophone
  74. ---
  75. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  76.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  77.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  78.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  79.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  80. ]
  81.